package com.example.dao.implDao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commons.exception.DuplicateUsernameException;
import com.example.dao.UserDao;
import com.example.mapper.UserMapper;
import com.example.pojo.Menu;
import com.example.pojo.TeacherInfo;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {
    private final JdbcTemplate jdbcTemplate;
    private final UserMapper userMapper;

    @Autowired
    public UserDaoImpl(UserMapper userMapper,JdbcTemplate jdbcTemplate) {
        this.userMapper = userMapper;
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }

    @Override
    public IPage<User> getUsersPage(int currentPage, int limit) {
        // 创建分页对象
        Page<User> page = new Page<>(currentPage, limit);
        return userMapper.selectPage(page,null);
    }

    @Override
    public User getUserBynameOne(String username) {
        String sql = "SELECT * FROM t_user WHERE username = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username);
    }

    @Override
    public IPage<User> getUserByname(int currentPage, int limit,String username) {
        Page<User> userPage = new Page<>(currentPage,limit);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.likeRight("username",username);
        return userMapper.selectPage(userPage, userQueryWrapper);
    }


    @Override
    public boolean login(String username, String password) {
        String sql = "SELECT * FROM t_user WHERE username = ? AND password = ?";
        try {
            User user = jdbcTemplate.queryForObject(sql, new Object[]{username, password}, new BeanPropertyRowMapper<>(User.class));
            // 如果查询到了用户，则认为登录成功
            return user != null;
        } catch (EmptyResultDataAccessException e) {
            // 没有找到匹配的用户名和密码，登录失败
            return false;
        }
    }

    @Override
    public int addUser(User user) {
       return userMapper.insert(user);
    }

    @Override
    public int toggleStatus(int uid, int checked) {
        String sql = "update t_user set status = ? where id = ?";
        return jdbcTemplate.update(sql,checked,uid);
    }

    @Override
    public int deleteUser(int uid) {
        return userMapper.deleteById(uid);
    }

    @Override
    public boolean getStateByName(String username) {
        String sql = "SELECT status FROM t_user WHERE username = ?";
        User user = jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<>(User.class));
        Integer status = user.getStatus();
        return status == 1;
    }

    @Override
    public List<Menu> getUserHaveMenu(Integer uid) {
        String sql = "SELECT p.* \n" +
                "FROM t_menu p \n" +
                "INNER JOIN t_role_menu rp ON p.id = rp.mid\n" +
                "INNER JOIN t_role r ON r.id = rp.rid\n" +
                "INNER JOIN t_user_role ur ON r.id = ur.rid \n" +
                "WHERE ur.uid = ?";
        List<Menu> menus = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Menu.class), uid);
        return MenuDaoImpl.filterMenuList(menus);
    }

    @Override
    public int batchDeleteUser(List<Integer> ids) {
        return userMapper.deleteBatchIds(ids);
    }

    @Override
    public int changeStatus(int uId, int uStatus) {
        return userMapper.changeStatus(uStatus,uId);
    }
}
